/**
 * 作者: wgx
 * 日期: 2025-05-06 16:24:38
 * 备注: 用户大世界的行军队列
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserWorldTeam;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserWorldTeamModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.team_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 用户Id
    protected int teamId; // 队列id，1-5，获得某个道具解锁一个
    protected int status; // 状态 (0-空闲中，1-过去途中，2-返回途中)
    protected int locationId; // 事件的坑位id (对应user_world_event_location的location_id)
    protected long startTime; // 行军的开始时间
    protected String partner = ""; // 派遣的干员列表
    protected int soldier; // 带的兵力
    protected long power; // 出征时的总战力
    protected int timeLeft; // 单程的剩余时间（秒）
    protected long updateTime; // 更新时间
    protected int eventId; // 事件id, world_event表的id
    protected int result; // 战斗结果 (0过期，1失败，2胜利3完胜)
    protected long forces; // 对方的战力
    protected int onewayTime; // 单程时间
    protected int coordX; // 坐标x
    protected int coordY; // 坐标x
    protected long helpDbid; // 联盟互助的dbid
    protected int reduceTime; // 别人减少的时间秒 (如联盟助力)
    protected String uniqeId = ""; // 唯一值，每次行军更新
    protected long totalForces; // 事件坑位初始时怪兽的总战力 (>0表示需要战斗)
    protected String randomReward = ""; // 随机奖励
    protected String param = ""; // 特殊参数 (如搜救事件1-表示当时刚好集齐秘书碎片)
    protected long validTime; // 有效时间。临时行军队列 (0表示无效)
    protected String boxReward = ""; // 全胜时的宝箱奖励，生成时就要确定奖励

    public void reset() {
    }

    protected String getKey() {
        return "UserWorldTeam." + userId + "." + teamId;
    }

    protected String getListKey() {
        return "UserWorldTeam." + userId;
    }

    protected String getListItemKey() {
        return teamId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_world_team"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 用户Id
        obj.put("teamId", teamId); // 队列id，1-5，获得某个道具解锁一个
        obj.put("status", status); // 状态 (0-空闲中，1-过去途中，2-返回途中)
        obj.put("locationId", locationId); // 事件的坑位id (对应user_world_event_location的location_id)
        obj.put("startTime", startTime); // 行军的开始时间
        obj.put("partner", partner); // 派遣的干员列表
        obj.put("soldier", soldier); // 带的兵力
        obj.put("power", power); // 出征时的总战力
        obj.put("timeLeft", timeLeft); // 单程的剩余时间（秒）
        obj.put("updateTime", updateTime); // 更新时间
        obj.put("eventId", eventId); // 事件id, world_event表的id
        obj.put("result", result); // 战斗结果 (0过期，1失败，2胜利3完胜)
        obj.put("forces", forces); // 对方的战力
        obj.put("onewayTime", onewayTime); // 单程时间
        obj.put("coordX", coordX); // 坐标x
        obj.put("coordY", coordY); // 坐标x
        obj.put("helpDbid", helpDbid); // 联盟互助的dbid
        obj.put("reduceTime", reduceTime); // 别人减少的时间秒 (如联盟助力)
        obj.put("uniqeId", uniqeId); // 唯一值，每次行军更新
        obj.put("totalForces", totalForces); // 事件坑位初始时怪兽的总战力 (>0表示需要战斗)
        obj.put("randomReward", randomReward); // 随机奖励
        obj.put("param", param); // 特殊参数 (如搜救事件1-表示当时刚好集齐秘书碎片)
        obj.put("validTime", validTime); // 有效时间。临时行军队列 (0表示无效)
        obj.put("boxReward", boxReward); // 全胜时的宝箱奖励，生成时就要确定奖励
        return obj;
    }

    public PBUserWorldTeam.Builder takeInitBuilder() {
        PBUserWorldTeam.Builder sb = PBUserWorldTeam.newBuilder();
        if (teamId != 0) {
            sb.setTeamId(teamId);
        }
        if (status != 0) {
            sb.setStatus(status);
        }
        if (locationId != 0) {
            sb.setLocationId(locationId);
        }
        if (startTime != 0) {
            sb.setStartTime(startTime);
        }
        if (this.partner != null && this.partner.length() > 0) {
            JsonArray list = new JsonArray(this.partner);
            for(int i=0; i<list.size(); i++) {
                sb.addPartner(list.getInteger(i));
            }
        }
        if (soldier != 0) {
            sb.setSoldier(soldier);
        }
        if (power != 0) {
            sb.setPower(power);
        }
        if (timeLeft != 0) {
            sb.setTimeLeft(timeLeft);
        }
        if (updateTime != 0) {
            sb.setUpdateTime(updateTime);
        }
        if (eventId != 0) {
            sb.setEventId(eventId);
        }
        if (result != 0) {
            sb.setResult(result);
        }
        if (onewayTime != 0) {
            sb.setOnewayTime(onewayTime);
        }
        if (coordX != 0) {
            sb.setCoordX(coordX);
        }
        if (coordY != 0) {
            sb.setCoordY(coordY);
        }
        if (reduceTime != 0) {
            sb.setReduceTime(reduceTime);
        }
        if (!uniqeId.isEmpty()) {
            sb.setUniqeId(uniqeId);
        }
        if (!param.isEmpty()) {
            sb.setParam(param);
        }
        if (validTime != 0) {
            sb.setValidTime(validTime);
        }
        return sb;
    }

    public PBUserWorldTeam.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserWorldTeam.Builder sb = PBUserWorldTeam.newBuilder();
        if (isFieldUpdate("teamId")) {
            sb.setTeamId(teamId);
        }
        if (isFieldUpdate("status")) {
            sb.setStatus(status);
        }
        if (isFieldUpdate("locationId")) {
            sb.setLocationId(locationId);
        }
        if (isFieldUpdate("startTime")) {
            sb.setStartTime(startTime);
        }
        if (isFieldUpdate("partner")) {
            if (this.partner != null && this.partner.length() > 0) {
                JsonArray list = new JsonArray(this.partner);
                for(int i=0; i<list.size(); i++) {
                    sb.addPartner(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearPartner();
                }
            } else {
                sb.clearPartner();
            }
        }
        if (isFieldUpdate("soldier")) {
            sb.setSoldier(soldier);
        }
        if (isFieldUpdate("power")) {
            sb.setPower(power);
        }
        if (isFieldUpdate("timeLeft")) {
            sb.setTimeLeft(timeLeft);
        }
        if (isFieldUpdate("updateTime")) {
            sb.setUpdateTime(updateTime);
        }
        if (isFieldUpdate("eventId")) {
            sb.setEventId(eventId);
        }
        if (isFieldUpdate("result")) {
            sb.setResult(result);
        }
        if (isFieldUpdate("onewayTime")) {
            sb.setOnewayTime(onewayTime);
        }
        if (isFieldUpdate("coordX")) {
            sb.setCoordX(coordX);
        }
        if (isFieldUpdate("coordY")) {
            sb.setCoordY(coordY);
        }
        if (isFieldUpdate("reduceTime")) {
            sb.setReduceTime(reduceTime);
        }
        if (isFieldUpdate("uniqeId")) {
            sb.setUniqeId(uniqeId);
        }
        if (isFieldUpdate("param")) {
            sb.setParam(param);
        }
        if (isFieldUpdate("validTime")) {
            sb.setValidTime(validTime);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getTeamId() {
        return teamId;
    }

    public void putTeamId(int teamId) {
        if (this.teamId != teamId) {
            this.teamId = teamId;
            updateRedisHashField(getKey(), "teamId", this.teamId);
        }
    }

    public void addTeamId(int value) {
        this.teamId += value;
        updateRedisHashField(getKey(), "teamId", this.teamId);
    }

    public int getStatus() {
        return status;
    }

    public void putStatus(int status) {
        if (this.status != status) {
            this.status = status;
            updateRedisHashField(getKey(), "status", this.status);
        }
    }

    public void addStatus(int value) {
        this.status += value;
        updateRedisHashField(getKey(), "status", this.status);
    }

    public int getLocationId() {
        return locationId;
    }

    public void putLocationId(int locationId) {
        if (this.locationId != locationId) {
            this.locationId = locationId;
            updateRedisHashField(getKey(), "locationId", this.locationId);
        }
    }

    public void addLocationId(int value) {
        this.locationId += value;
        updateRedisHashField(getKey(), "locationId", this.locationId);
    }

    public long getStartTime() {
        return startTime;
    }

    public void putStartTime(long startTime) {
        if (this.startTime != startTime) {
            this.startTime = startTime;
            updateRedisHashField(getKey(), "startTime", this.startTime);
        }
    }

    public void addStartTime(long value) {
        this.startTime += value;
        updateRedisHashField(getKey(), "startTime", this.startTime);
    }

    public JsonArray getPartnerArray() {
        if (this.partner != null && this.partner.length() > 0) {
            return new JsonArray(this.partner);
        } else {
            return new JsonArray();
        }
    }

    public String getPartner() {
        return partner;
    }

    public void putPartner(String partner) {
        if (!this.partner.equals(partner)) {
            this.partner = partner;
            updateRedisHashField(getKey(), "partner", this.partner);
        }
    }

    public int getSoldier() {
        return soldier;
    }

    public void putSoldier(int soldier) {
        if (this.soldier != soldier) {
            this.soldier = soldier;
            updateRedisHashField(getKey(), "soldier", this.soldier);
        }
    }

    public void addSoldier(int value) {
        this.soldier += value;
        updateRedisHashField(getKey(), "soldier", this.soldier);
    }

    public long getPower() {
        return power;
    }

    public void putPower(long power) {
        if (this.power != power) {
            this.power = power;
            updateRedisHashField(getKey(), "power", this.power);
        }
    }

    public void addPower(long value) {
        this.power += value;
        updateRedisHashField(getKey(), "power", this.power);
    }

    public int getTimeLeft() {
        return timeLeft;
    }

    public void putTimeLeft(int timeLeft) {
        if (this.timeLeft != timeLeft) {
            this.timeLeft = timeLeft;
            updateRedisHashField(getKey(), "timeLeft", this.timeLeft);
        }
    }

    public void addTimeLeft(int value) {
        this.timeLeft += value;
        updateRedisHashField(getKey(), "timeLeft", this.timeLeft);
    }

    public long getUpdateTime() {
        return updateTime;
    }

    public void putUpdateTime(long updateTime) {
        if (this.updateTime != updateTime) {
            this.updateTime = updateTime;
            updateRedisHashField(getKey(), "updateTime", this.updateTime);
        }
    }

    public void addUpdateTime(long value) {
        this.updateTime += value;
        updateRedisHashField(getKey(), "updateTime", this.updateTime);
    }

    public int getEventId() {
        return eventId;
    }

    public void putEventId(int eventId) {
        if (this.eventId != eventId) {
            this.eventId = eventId;
            updateRedisHashField(getKey(), "eventId", this.eventId);
        }
    }

    public void addEventId(int value) {
        this.eventId += value;
        updateRedisHashField(getKey(), "eventId", this.eventId);
    }

    public int getResult() {
        return result;
    }

    public void putResult(int result) {
        if (this.result != result) {
            this.result = result;
            updateRedisHashField(getKey(), "result", this.result);
        }
    }

    public void addResult(int value) {
        this.result += value;
        updateRedisHashField(getKey(), "result", this.result);
    }

    public long getForces() {
        return forces;
    }

    public void putForces(long forces) {
        if (this.forces != forces) {
            this.forces = forces;
            updateRedisHashField(getKey(), "forces", this.forces);
        }
    }

    public void addForces(long value) {
        this.forces += value;
        updateRedisHashField(getKey(), "forces", this.forces);
    }

    public int getOnewayTime() {
        return onewayTime;
    }

    public void putOnewayTime(int onewayTime) {
        if (this.onewayTime != onewayTime) {
            this.onewayTime = onewayTime;
            updateRedisHashField(getKey(), "onewayTime", this.onewayTime);
        }
    }

    public void addOnewayTime(int value) {
        this.onewayTime += value;
        updateRedisHashField(getKey(), "onewayTime", this.onewayTime);
    }

    public int getCoordX() {
        return coordX;
    }

    public void putCoordX(int coordX) {
        if (this.coordX != coordX) {
            this.coordX = coordX;
            updateRedisHashField(getKey(), "coordX", this.coordX);
        }
    }

    public void addCoordX(int value) {
        this.coordX += value;
        updateRedisHashField(getKey(), "coordX", this.coordX);
    }

    public int getCoordY() {
        return coordY;
    }

    public void putCoordY(int coordY) {
        if (this.coordY != coordY) {
            this.coordY = coordY;
            updateRedisHashField(getKey(), "coordY", this.coordY);
        }
    }

    public void addCoordY(int value) {
        this.coordY += value;
        updateRedisHashField(getKey(), "coordY", this.coordY);
    }

    public long getHelpDbid() {
        return helpDbid;
    }

    public void putHelpDbid(long helpDbid) {
        if (this.helpDbid != helpDbid) {
            this.helpDbid = helpDbid;
            updateRedisHashField(getKey(), "helpDbid", this.helpDbid);
        }
    }

    public void addHelpDbid(long value) {
        this.helpDbid += value;
        updateRedisHashField(getKey(), "helpDbid", this.helpDbid);
    }

    public int getReduceTime() {
        return reduceTime;
    }

    public void putReduceTime(int reduceTime) {
        if (this.reduceTime != reduceTime) {
            this.reduceTime = reduceTime;
            updateRedisHashField(getKey(), "reduceTime", this.reduceTime);
        }
    }

    public void addReduceTime(int value) {
        this.reduceTime += value;
        updateRedisHashField(getKey(), "reduceTime", this.reduceTime);
    }

    public String getUniqeId() {
        return uniqeId;
    }

    public void putUniqeId(String uniqeId) {
        if (!this.uniqeId.equals(uniqeId)) {
            this.uniqeId = uniqeId;
            updateRedisHashField(getKey(), "uniqeId", this.uniqeId);
        }
    }

    public long getTotalForces() {
        return totalForces;
    }

    public void putTotalForces(long totalForces) {
        if (this.totalForces != totalForces) {
            this.totalForces = totalForces;
            updateRedisHashField(getKey(), "totalForces", this.totalForces);
        }
    }

    public void addTotalForces(long value) {
        this.totalForces += value;
        updateRedisHashField(getKey(), "totalForces", this.totalForces);
    }

    public String getRandomReward() {
        return randomReward;
    }

    public void putRandomReward(String randomReward) {
        if (!this.randomReward.equals(randomReward)) {
            this.randomReward = randomReward;
            updateRedisHashField(getKey(), "randomReward", this.randomReward);
        }
    }

    public String getParam() {
        return param;
    }

    public void putParam(String param) {
        if (!this.param.equals(param)) {
            this.param = param;
            updateRedisHashField(getKey(), "param", this.param);
        }
    }

    public long getValidTime() {
        return validTime;
    }

    public void putValidTime(long validTime) {
        if (this.validTime != validTime) {
            this.validTime = validTime;
            updateRedisHashField(getKey(), "validTime", this.validTime);
        }
    }

    public void addValidTime(long value) {
        this.validTime += value;
        updateRedisHashField(getKey(), "validTime", this.validTime);
    }

    public String getBoxReward() {
        return boxReward;
    }

    public void putBoxReward(String boxReward) {
        if (!this.boxReward.equals(boxReward)) {
            this.boxReward = boxReward;
            updateRedisHashField(getKey(), "boxReward", this.boxReward);
        }
    }

}